package com.Movies;

import java.util.Scanner;

/*
要写注释哦!!!
*/
public class MoviesOperator {
    Movies[] movies = new Movies[3];//定义全局数组来存放数据

    /**
     * 构造方法用来写入数据，每当new对象时就会执行该方法，这个方法最终是用来读取数据库的
     */
    public MoviesOperator() {
        movies[0] = new Movies(1, "妈妈再爱我一次", 9.8, "张艺谋", "路人甲");
        movies[1] = new Movies(2, "活着", 9.8, "张艺谋", "路人甲");
        movies[2] = new Movies(3, "1942", 9.8, "张艺谋", "路人甲");
    }

    /**
     * start（）方法使用来掉用所有的方法的
     */
    public void stare() {
        wc:
        while (true) {
            //提示用户输入操作命令
            System.out.println("欢迎来到电影系统~~");
            System.out.println("请输入操作命令：");
            System.out.println("1、查看所有的电影信息");
            System.out.println("2、根据评分范围查找电影");
            System.out.println("3、根据id查找电影");
            System.out.println("4、退出");
            Scanner sc = new Scanner(System.in);
            int chooser = sc.nextInt();

            switch (chooser) {
                case 1:
                    showAllMovies(movies);
                    break;
                case 2:
                    System.out.println("请输入最小范围：");
                    double star = sc.nextDouble();
                    System.out.println("请输入最大范围：");
                    double end = sc.nextDouble();
                    printMoviesByrate(movies, star, end);
                    break;
                case 3:
                    System.out.println("请输入id:");
                    int id = sc.nextInt();
                    printMoviesById(movies, id);
                    break;
                case 4:
                    System.out.println("欢迎下次再来~~~");
                    break wc;
                default:
                    System.out.println("输入的命令有误~~~");
                    break;
            }
        }
    }

    /**
     * 根据评分范围查电影
     *
     * @param movies 传入所有的电影信息
     * @param star   传入评分最小值
     * @param end    传入评分最大值
     */
    public void printMoviesByrate(Movies[] movies, double star, double end) {
        int count = 0;
        for (int i = 0; i < movies.length; i++) {
            Movies movies3 = movies[i];
            if (movies3.getRate() >= star && (movies3.getRate() <= end)) {
                count++;
                System.out.println("电影的编号：" + movies3.getId());
                System.out.println("电影的名字：" + movies3.getName());
                System.out.println("电影的评分：" + movies3.getRate());
                System.out.println("电影的导演：" + movies3.getLeader());
                System.out.println("电影的主演：" + movies3.getActor());
                System.out.println("===================");
            }
        }
        if (count == 0) {
            System.out.println("没有评分" + star + "到" + end + "的电影信息");
        }
    }

    /**
     * 输出所有的电影信息
     *
     * @param movies 传入电影信息的数组
     */
    public void showAllMovies(Movies[] movies) {
        for (int i = 0; i < movies.length; i++) {
            Movies movies2 = movies[i];
            System.out.println("电影的编号：" + movies2.getId());
            System.out.println("电影的名字：" + movies2.getName());
            System.out.println("电影的评分：" + movies2.getRate());
            System.out.println("电影的导演：" + movies2.getLeader());
            System.out.println("电影的主演：" + movies2.getActor());
            System.out.println("==================");
        }
    }

    /**
     * 根据Id查询电影信息
     *
     * @param movies 传入数组
     * @param id     传入要查的id
     */
    public void printMoviesById(Movies[] movies, int id) {
        Movies temp = null;
        for (int i = 0; i < movies.length; i++) {
            Movies movies1 = movies[i];
            if (movies1.getId() == id) {
                temp = movies1;
                break;
            }
        }
        if (temp == null) {
            System.out.println("电影不存在！");
        } else {
            System.out.println("电影的编号：" + temp.getId());
            System.out.println("电影的名字：" + temp.getName());
            System.out.println("电影的评分：" + temp.getRate());
            System.out.println("电影的导演：" + temp.getLeader());
            System.out.println("电影的主演：" + temp.getActor());
        }
    }
}
